{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1、导入可创建 Excel的工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# xlwt 工具包可以对 xls 文件进行写操作\n",
    "# xlwt 的命名是 excel writer 简写，很容易记忆\n",
    "import xlwt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果本地没有安装工具包，请执行\n",
    "!pip install -i https://mirrors.aliyun.com/pypi/simple/ xlwt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2、创建一个工作簿"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<font color=blue>我们准备创建Excel如下</font>**<br/>\n",
    "<img src=\"images/xlwt_01.png\" align=\"left\"></img>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在内存中创建一个工作簿（workbook），并设置编码为utf-8（可以忽略，有兴趣可以百度“UTF8编码”）\n",
    "workbook = xlwt.Workbook(encoding='utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3、创建一个工作表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在工作簿（workbook）中，新增一个工作表（sheet）,并命名为“我的工作表”\n",
    "sheet = workbook.add_sheet('我的工作表')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4、在工作表中写入文字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在工作表（sheet）中写入文字\n",
    "# 写入方法（write）需要给出 行、列、文字内容 3个关键信息，我们称为参数\n",
    "# 参数 r => row 行，c => column 列，label => 文字标签 \n",
    "sheet.write(r=0, c=0, label='序号')\n",
    "sheet.write(r=0, c=1, label='姓名')\n",
    "sheet.write(r=0, c=2, label='性别')\n",
    "sheet.write(r=0, c=3, label='年龄')\n",
    "# 以上代码完成了在第一行（r=0）写入标题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 写入第1个人的信息\n",
    "sheet.write(r=1, c=0, label=1)\n",
    "sheet.write(r=1, c=1, label='张三风')\n",
    "sheet.write(r=1, c=2, label='男')\n",
    "sheet.write(r=1, c=3, label=80)\n",
    "# 以上代码完成了在第二行（r=1）写入“张三风”的信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 写入第2个人的信息\n",
    "sheet.write(r=2, c=0, label=2)\n",
    "sheet.write(r=2, c=1, label='黄小妹')\n",
    "sheet.write(r=2, c=2, label='女')\n",
    "sheet.write(r=2, c=3, label=18)\n",
    "# 以上代码完成了在第二行（r=2）写入“黄小妹”的信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5、保存Excel文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存工作簿（workbook）到指定文件路径名\n",
    "workbook.save('./files/Excel_写入用户信息.xls')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<font color=blue>Excel创建完成如下</font>**<br/>\n",
    "<img src=\"images/xlwt_02.png\" align=\"left\"></img>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<font color=blue size=4> 1、什么是参数？</font>**\n",
    "\n",
    "<font color=blue size=4> 参数 r => row 行，c => column 列，label => 文字标签  </font>\n",
    "\n",
    "<font color=red size=4> sheet.write(r=0, c=0, label='序号') </font>\n",
    "\n",
    "<font color=blue size=4> 比如：拿猫吃东西举例\n",
    "    \n",
    "吃鱼的代码： <font color=red size=4>cat.eat(something=fish) </font> \n",
    "    \n",
    "吃老鼠的代码： <font color=red size=4>cat.eat(something=mouse)</font> </font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<font color=blue size=4> 2、什么是数据类型？</font>**\n",
    "\n",
    "<font color=red size=4> sheet.write(r=2, c=0, label=2) </font>\n",
    "\n",
    "<font color=red size=4> sheet.write(r=2, c=1, label='黄小妹') </font>\n",
    "\n",
    "\n",
    "<font color=blue size=4>\n",
    "看看label=2 和 label='黄小妹'，“2”两边没有单引号，而“黄小妹”两边有单引号。</font>\n",
    "\n",
    "<font color=blue size=4>我们把“2”称为数字类型，把带有引号的“黄小妹”成为字符串类型，精确的说应该是中文字符串。</font>\n",
    "\n",
    "<font color=blue size=4>当然还有英文字符串，比如：“sheet”，我们看看“sheet”加引号和不加引号的区别 </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 工作表对象\n",
    "print(sheet)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串类型\n",
    "print(\"sheet\") # 可以用双引号\n",
    "print('sheet') # 可以用单引号\n",
    "print(\"\"\"sheet\"\"\") # 可以用三个双引号，以后再了解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "sheet 是对象(Worksheet object)，\"sheet\" 是字符串，我们就用引号来区分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=blue size=4>Python提供常用的数据类型有：字符串、整型、浮点型、列表、元组、字典、布尔型等等，</font>\n",
    "\n",
    "<font color=blue size=4>补充一下，整型就是整数，浮点型就是小数，属于数值类型，其他类型我们以后慢慢讲到。</font>\n",
    "\n",
    "<font color=blue size=4>请大家记住这个词“数据类型”</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 工作表对象\n",
    "type(sheet)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串类型\n",
    "type(\"sheet\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 完整代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# xlwt 工具包可以对 xls 文件进行写操作\n",
    "# xlwt 的命名是 excel writer 简写，很容易记忆\n",
    "import xlwt\n",
    "\n",
    "# 在内存中创建一个工作簿（workbook），并设置编码为utf-8（可以忽略，有兴趣可以百度“UTF8编码”）\n",
    "workbook = xlwt.Workbook(encoding='utf-8')\n",
    "# 在工作簿（workbook）中，新增一个工作表（sheet）,并命名为“我的工作表”\n",
    "sheet = workbook.add_sheet('我的工作表')\n",
    "# 以上代码完成了在第一行（r=0）写入标题\n",
    "sheet.write(r=0, c=0, label='序号')\n",
    "sheet.write(r=0, c=1, label='姓名')\n",
    "sheet.write(r=0, c=2, label='性别')\n",
    "sheet.write(r=0, c=3, label='年龄')\n",
    "# 在第二行（r=1）写入“张三风”的信息\n",
    "sheet.write(r=1, c=0, label=1)\n",
    "sheet.write(r=1, c=1, label='张三风')\n",
    "sheet.write(r=1, c=2, label='男')\n",
    "sheet.write(r=1, c=3, label=80)\n",
    "# 在第二行（r=2）写入“黄小妹”的信息\n",
    "sheet.write(r=2, c=0, label=2)\n",
    "sheet.write(r=2, c=1, label='黄小妹')\n",
    "sheet.write(r=2, c=2, label='女')\n",
    "sheet.write(r=2, c=3, label=18)\n",
    "# 保存工作簿（workbook）到指定文件路径名\n",
    "workbook.save('./files/Excel_写入用户信息.xls')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思考题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1）可以看到代码有很多相似的，怎么优化代码呢？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "目前是只增加2行数据。但是未来我们需要增加更多行的时候，那上面的代码就不是好代码，我们看怎么优化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# xlwt 工具包可以对 xls 文件进行写操作\n",
    "# xlwt 的命名是 excel writer 简写，很容易记忆\n",
    "import xlwt\n",
    "\n",
    "# 在内存中创建一个工作簿（workbook），并设置编码为utf-8（可以忽略，有兴趣可以百度“UTF8编码”）\n",
    "workbook = xlwt.Workbook(encoding='utf-8')\n",
    "# 在工作簿（workbook）中，新增一个工作表（sheet）,并命名为“我的工作表”\n",
    "sheet = workbook.add_sheet('我的工作表')\n",
    "\n",
    "\n",
    "# ************************************\n",
    "# ** 我们定义了功能函数 write_sheet **\n",
    "def write_sheet(row, id, name, gender, age):\n",
    "    sheet.write(r=row, c=0, label=id)\n",
    "    sheet.write(r=row, c=1, label=name)\n",
    "    sheet.write(r=row, c=2, label=gender)\n",
    "    sheet.write(r=row, c=3, label=age)\n",
    "# ************************************\n",
    "\n",
    "# 第一行（r=0）写入标题\n",
    "write_sheet(0, '序号', '姓名', '性别', '年龄')\n",
    "write_sheet(1, 1, '张三风', '男', 80)\n",
    "write_sheet(2, 2, '黄小妹', '女', 18)\n",
    "write_sheet(3, 3, '黄飞红', '女', 28)\n",
    "write_sheet(4, 4, '李飞', '男', 20)\n",
    "\n",
    "# 保存工作簿（workbook）到指定文件路径名\n",
    "workbook.save('./files/Excel_写入用户信息_2.xls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# xlwt 工具包可以对 xls 文件进行写操作\n",
    "# xlwt 的命名是 excel writer 简写，很容易记忆\n",
    "import xlwt\n",
    "\n",
    "# 在内存中创建一个工作簿（workbook），并设置编码为utf-8（可以忽略，有兴趣可以百度“UTF8编码”）\n",
    "workbook = xlwt.Workbook(encoding='utf-8')\n",
    "# 在工作簿（workbook）中，新增一个工作表（sheet）,并命名为“我的工作表”\n",
    "sheet = workbook.add_sheet('我的工作表')\n",
    "\n",
    "\n",
    "# ************************************\n",
    "# ** 我们定义了功能函数 write_sheet **\n",
    "def write_sheet(row, values):\n",
    "    for i in range(4):\n",
    "        sheet.write(r=row, c=i, label=values[i])\n",
    "# ************************************\n",
    "\n",
    "# 第一行（r=0）写入标题\n",
    "write_sheet(0, ['序号', '姓名', '性别', '年龄'])\n",
    "write_sheet(1, [1, '张三风', '男', 80])\n",
    "write_sheet(2, [2, '黄小妹', '女', 18])\n",
    "write_sheet(3, [3, '黄飞红', '女', 28])\n",
    "write_sheet(4, [4, '李飞', '男', 20])\n",
    "\n",
    "# 保存工作簿（workbook）到指定文件路径名\n",
    "workbook.save('./files/Excel_写入用户信息_3.xls')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2）猜一下不同类型的加法运算结果？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1+2) # 数值类型（整型）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('1'+'2') # 字符串类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print([1]+[2]) # 列表类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(['1']+['2']) # 列表类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
